using System;
using System.Text;

namespace PropertyExpression.PresentationCode.CodeGenerator
{
    internal class AttachedPropertyOverloadedAndPrefixMethodsBuilder : IOverloadedAndPrefixMethodsBuilder
    {
        private readonly string methodName;
        private readonly Type targetType;

        public AttachedPropertyOverloadedAndPrefixMethodsBuilder(string methodName, Type targetType)
        {
            this.methodName = methodName;
            this.targetType = targetType;
        }

        public void BuildOverloadedMethods(Type parameterType, StringBuilder stringBuilder, string signatureParametersString, string invokeParametersString)
        {
            stringBuilder.AppendFormat(
                @"        public static T {0}<T>(this T it, {1}) where T: {4}
        {{
            return it.{0}(new {2}({3}));
        }}
",
                methodName,
                signatureParametersString,
                InternalUtil.GetCSharTypeName(parameterType),
                invokeParametersString,
                targetType);
        }

        public void BuildMethodsWithPrefix(StringBuilder methodsBuilder, string prefix, Type holderType)
        {
            methodsBuilder.AppendFormat(
                @"        public static T {0}_{1}<T>(this T it) where T: {3}
        {{
            return it.{0}({2}.{1});
        }}
",
                methodName, prefix, holderType, targetType);
        }
    }
}